package us.mooresr.aircraftscheduling.client.ui;

import com.gwtplatform.mvp.client.ViewImpl;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class LoginView extends ViewWithUiHandlers<LoginUiHandlers>
		implements LoginPresenter.MyView {

	private final Widget widget;
	private final Image waitImage;
	
	@UiField
	HTMLPanel panel;
	
	@UiField
	TextBox user;
	
	@UiField
	PasswordTextBox password;

	public interface Binder extends UiBinder<Widget, LoginView> {
	}

	@Inject
	public LoginView(final Binder binder) {
		widget = binder.createAndBindUi(this);
		waitImage = new Image("ajax-loader.gif");
		waitImage.addStyleName("waitImage");
	}

	@Override
	public Widget asWidget() {
		return widget;
	}
	
	@UiHandler("loginButton")
	void onSubmitClicked(ClickEvent event) {
		
		getUiHandlers().doLogin(user.getText(), password.getText());
	
	}
	
	@UiHandler("user")
	void onUserNameKey(KeyDownEvent e) {
		if (e.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
			password.setFocus(true);
		}	
	}
	
	@UiHandler("password")
	void onPasswordKey(KeyDownEvent e) {
		if (e.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
			getUiHandlers().doLogin(user.getText(), password.getText());
		}	
	}

	@Override
	public void setBusyCursor() {
		panel.add(waitImage);
		
	}

	@Override
	public void setNormalCursor() {
		panel.remove(waitImage);
		
		
	}

	@Override
	public void clear() {
		user.setText("");
		password.setText("");
		
	}
}
